<?php

class Useradmin extends record
{
	
	function __construct()
	{
		parent::__construct();
		$this->namaTabel = 'user';
	}	
	function getData($limit = null,$start=null){
		$this->get($fields = '*', array(), $limit , $start, 
			'waktu','DESC',null) ;
		return $this->execute();		
	}
	function getAllUser($limit = null,$start=null){
		$this->get($fields = '*', array(), $limit , $start,'waktu','DESC',null);
        $res =  $this->execute();
        $result = isset($res['result'])?$res['result']:array();
        $this->num_rows();
        $nr  =  $this->execute();
		$total = isset($nr['result'])?$nr['result'][0]['total']:0;
        return array('total'=>$total,'result'=>$result);
	}
	function makeCombo(){
		$er = $this->getAllUser();
		$str = '<option></option>';
		foreach ($er['result'] as $mkC){
			$str .="<option value='".$mkC['id']."'>".$mkC['nama']." (".$mkC['username'].")</option>";
		}
		return $str;
	}
	function meanJabatan($idJab){
		switch ($idJab) {
			case '1':
			$re = "Super Administrasi";
			break;
			case '2':
			$re = 'Administrasi Pemesanan';
			break;
			default:
			$re = '(belum ditentukan)';
			break;
		}
		return $re;
	}
	function cekUser($us,$email){
		$this->get("*",null,2,null,null,null,"username='".$us."' OR email='".$email."'");
		$c = $this->execute();
		if(($c['success']==true && $c['total']==0 ) || $c['success']==false){
			return array('ada'=>false);
		}
		else{
			if($c['result'][0]['email']==$email){
				$err = 'Email sudah pernah digunakan';
			}
			else{
				$err = 'Username sudah pernah digunakan';
			}
			return array('ada'=>true,'error_msg'=>$err);
		}
	}
	function resetSessionBasedId($id){
		$this->get('*',array('id'=>$id));
		$dt=$this->execute();		
		if($dt['success']==false ){
			$ret= $dt;
		}
		else{
			if($dt['total']>0){
				$result = $dt['result'][0];
				$_SESSION['adminlogin']['logged_in']=true;
				$_SESSION['adminlogin']['alamat']=$result['alamat'];
				$_SESSION['adminlogin']['email']=$result['email'];
				$_SESSION['adminlogin']['id']=$result['id'];
				$_SESSION['adminlogin']['jabatan']=$result['jabatan'];
				$_SESSION['adminlogin']['lahir']=$result['lahir'];
				$_SESSION['adminlogin']['nama']=$result['nama'];
				$_SESSION['adminlogin']['username']=$result['username'];
				$_SESSION['adminlogin']['waktu']=$result['waktu'];
			}
			$ret = ($dt['total']>0)?array('success'=>true,'lihat'=>$result):array('success'=>false,'error_msg'=>'Username atau Password salah');
		}
		return $ret;
	}
	function doLogin($user,$pass){
		$this->get('*',array('password'=>$pass,'aktif'=>1),1,null,null,null,"(email='".$user."' OR username = '".$user."')");
		$dt = $this->execute();
		if($dt['success']==false ){
			$ret= $dt;
		}
		else{
			if($dt['total']>0){
				$result = $dt['result'][0];
				$_SESSION['adminlogin']['logged_in']=true;
				$_SESSION['adminlogin']['alamat']=$result['alamat'];
				$_SESSION['adminlogin']['email']=$result['email'];
				$_SESSION['adminlogin']['id']=$result['id'];
				$_SESSION['adminlogin']['jabatan']=$result['jabatan'];
				$_SESSION['adminlogin']['lahir']=$result['lahir'];
				$_SESSION['adminlogin']['nama']=$result['nama'];
				$_SESSION['adminlogin']['username']=$result['username'];
				$_SESSION['adminlogin']['waktu']=$result['waktu'];
			}
			$ret = ($dt['total']>0)?array('success'=>true,'lihat'=>$result):array('success'=>false,'error_msg'=>'Username atau Password salah');
		}
		return $ret;

	}
}
$objUseradmin = new Useradmin();